package com.ruoyi.system.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.Gson;
import com.ruoyi.system.constant.Constants;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class draw {


    public static void main(String[] args) throws JsonProcessingException {
    }



    public static void overpng(String json,String pathname,int wid,int hei) throws JsonProcessingException{
        try {
            // 使用Gson库将JSON数据转换为Java对象
            Gson gson = new Gson();
            Point[] points = gson.fromJson(json, Point[].class);
            for(int i=0;i<points.length;i++){
                if(points[i].stroke_start&&i!=0){
                    points[i-1].stroke_end=true;
                }
            }
            points[points.length-1].stroke_end=true;
            BufferedImage image = processImage(points,wid,hei);
            File output = new File(pathname);
            ImageIO.write(image, "png", output);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void overpngbyX_Y(String small,String big,int X,int Y,String out){
        try {

            BufferedImage bigImage = ImageIO.read(new File(big));
            BufferedImage smallImage = ImageIO.read(new File(small));

            Graphics2D g2d = bigImage.createGraphics();
            int x = X; // x-coordinate for the top-left corner of the small image
            int y = Y; // y-coordinate for the top-left corner of the small image
            g2d.drawImage(smallImage, x, y, null);
            g2d.dispose();

            // Save the resulting image
            File outputFile = new File(out);
            ImageIO.write(bigImage, "PNG", outputFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static BufferedImage processImage(Point[] points,int wid,int hei) {

        // 计算图像宽度和高度
        int width = wid;
        int height = hei;

        int minX = 0;
        int minY =0;


        // 创建图像
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = image.createGraphics();

        // 配置绘图参数
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.BLACK);
        // 绘制每个点之间的连线
        for (int i = 1; i < points.length; i++) {
            Point prevPoint = points[i - 1];
            Point currPoint = points[i];
            if (!prevPoint.stroke_end) {
                int px = (int) Math.round(prevPoint.x*11.819955);
                int py = (int) Math.round(prevPoint.y*11.819955);
                int ex = (int) Math.round(currPoint.x*11.819955);
                int ey = (int) Math.round(currPoint.y*11.819955);
                int startX = px - minX;
                int startY = py - minY;
                int endX = ex - minX;
                int endY = ey - minY;
                // 动态计算线条宽度

                float PENWIDTH_MIN = .1f;
                float StrokeWidthFactor = 4f;
                float pwidth = 0;
                float strokeWidth = PENWIDTH_MIN + StrokeWidthFactor * points[i].linewidth + pwidth;
                // 定义线条样式
                BasicStroke stroke = new BasicStroke(strokeWidth);
                g2d.setStroke(stroke);
                // 绘制线条
                g2d.drawLine(startX, startY, endX, endY);
            }
        }

        g2d.dispose();

        return image;
    }


    // 定义一个内部类Point用于解析JSON数据
    private static class Point {
        float linewidth;
        boolean stroke_start;
        boolean stroke_end;
        double time_stamp;
        double x;
        double y;
    }
}
